<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsCategoryBean {
	
	public function __construct() {
	}
		
	public function __destruct() {
		//
	}
	
    public static function get_category_by_condition($db, $cond) {
		$locale = $_SESSION["LC_ALL"];
    	$strSQL = sprintf("SELECT * FROM %s" .
				" WHERE ($cond) AND lang_id='$locale' AND cat_is_active = '1' ORDER BY cat_position ", 
				TBL_CATEGORY);
    	$res = $db->get_results($strSQL);
    	return $res;
    }
    
	public static function get_path_by_catid($db, $id) {
    	$strSQL = sprintf("SELECT banner_path FROM ".TBL_CATEGORY.
				" WHERE cat_id = '$id'");
    	$res = $db->get_row($strSQL);
    	return $res;
    }
	
	public static function get_catid_by_parent($db, $id) {
    	$strSQL = sprintf("SELECT cat_id,cat_name FROM ".TBL_CATEGORY.
				" WHERE cat_parent = '$id'");
    	$res = $db->get_results($strSQL);
    	return $res;
    }
	
	public static function get_category_by_level($db, $id,$nowid) {
    	$strSQL = sprintf("SELECT * FROM %1\$s" .
				" WHERE cat_parent = '$id' AND cat_id != '$nowid' ORDER BY cat_position ", TBL_CATEGORY);
    	$res = $db->get_results($strSQL);
    	return $res;
    }
	
   	public static function get_category_by_id($db, $id) {
    	$strSQL = sprintf("SELECT * FROM %1\$s" .
				" WHERE cat_id = '$id'", TBL_CATEGORY);
    	$res = $db->get_row($strSQL);
    	return $res;
    }
    
	public static function get_catandparent_by_id($db, $id) {
    	$strSQL = sprintf("SELECT c.cat_name,p.cat_name as parentname FROM ".TBL_CATEGORY." c ".
				" LEFT JOIN ".TBL_CATEGORY." p ON p.cat_id=c.cat_parent".
				" WHERE c.cat_id = '$id'");
    	$res = $db->get_row($strSQL);
    	return $res;
    }
	
    public static function get_category_name_by_id($db, $id, $parent_id = 0) {
    	$strSQL = sprintf("SELECT cat_id, cat_name FROM %1\$s" .
				" WHERE cat_id = '$id' AND cat_parent = '$parent_id'", TBL_CATEGORY);
    	$res = $db->get_row($strSQL);
    	$result[] = $res->cat_name;
    	if ($parent_id != 0) {
    		$pr = BgcmsCategoryBean::get_parent_id_name_by_id($db, $id);
    		$strSQL1 = BgcmsCategoryBean::get_category_name_by_id($db,$pr);
    	}
		$res = $db->get_row($strSQL1);
		$result[] = $res->cat_name;
		var_dump($result);
    	return $result;
    }    
    
   	public static function get_parent_id_name_by_id($db, $id) {
    	$strSQL = sprintf("SELECT cat_parent FROM %1\$s" .
				" WHERE cat_id = '$id'", TBL_CATEGORY);
    	$res = $db->get_var($strSQL);
    	return $res;
    }    
    
    public static function get_all_category($db, $ignoreID) {
		$catArray = array();
		//get all root category
		$res = BgcmsCategoryBean::get_static_category($db, 'cat_parent=0 AND cat_ontop = 1');
		
		if($res == null) return null;
		
		// with each root, we get all sub category of it
		foreach($res as $cat) {
			if($ignoreID == null || $ignoreID != $cat->cat_id) {
				$cat->temp_name = $cat->cat_name;
				array_push($catArray, $cat);
				BgcmsCategoryBean::get_category($db, $catArray, $cat, ATWEEN_HYPHEN, $ignoreID);
			}
		}
		return $catArray;
    }
    
    public static function get_static_category($db, $cond){
    	$strSQL = sprintf("SELECT * FROM %s " .
				"WHERE ($cond) AND cat_is_active = '1' ORDER BY cat_position", TBL_CATEGORY);
    	$res = $db->get_results($strSQL);
    	return $res;
    }
    
	public function get_category($db, &$arr, $cat, $tab, $ignoreID) {
		if($cat != null) {
			$locale = $_SESSION["LC_ALL"];
    		$res = BgcmsCategoryBean::get_category_by_condition($db, 
							'cat_parent=' . $cat->cat_id . " AND lang_id='$locale'" );
    		if($res != null) {
	    		foreach($res as $catsub) {
					if($ignoreID == null || $ignoreID != $catsub->cat_id) {
						$catsub->temp_name  = $tab . ' ' . $catsub->cat_name; 
						array_push($arr, $catsub);
						BgcmsCategoryBean::get_category($db, $arr, $catsub, $tab . ' ' . ATWEEN_HYPHEN, $ignoreID);
					}
				}
    		}
    	}
    	return $arr;
    }
    
	public static function save_category($db, $cat)
    {
    	$res = $db->query(sprintf(
					"INSERT INTO %s(cat_name,banner_path, cat_parent, cat_description, lang_id,cat_position,cat_ontop,cat_onleft,cat_is_active,cat_created_date) " .
					"VALUES('%s','%s', %d, '%s', '%s','%s', '%s', '%s','%s',now())",
    				TBL_CATEGORY, 
    				$cat['cat_name'],
					$cat['banner_path'],
    				$cat['cat_parent'],
    				$cat['cat_description'],
    				$_SESSION["LC_ALL"],
					$cat['cat_position'],
					$cat['cat_ontop'],
					$cat['cat_onleft'],
					$cat['cat_is_active']));
    	
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    }
	
    public static function update_category($db, $cat) {
    	$strSQL = sprintf("UPDATE  %s SET cat_name = '%s', " .
    					"banner_path = '%s', " .
						"cat_description = '%s', " .
    					"cat_parent='%d', " .
    					"cat_ontop='%d', " .
    					"cat_onleft='%d', " .
    					"cat_is_active='%d' " .
    					"WHERE cat_id = '%d'", 
					TBL_CATEGORY,
					mysql_real_escape_string($cat['cat_name']), 
					mysql_real_escape_string($cat['banner_path']), 
					mysql_real_escape_string($cat['cat_description']),
					mysql_real_escape_string($cat['cat_parent']),
					mysql_real_escape_string($cat['cat_ontop']),
					mysql_real_escape_string($cat['cat_onleft']),
					mysql_real_escape_string($cat['cat_is_active']),
					mysql_real_escape_string($cat['cat_id']));
		$res = $db->query($strSQL);

    	return $res;
    }
	
	/* Input: 	- id and value avtive of category need set active
	
	*/
	
	 public static function update_active_category($db, $cat,$value) {
    	$strSQL = sprintf("UPDATE  %s SET cat_is_active='%d' " .
    					"WHERE cat_id = '%d'", 
					TBL_CATEGORY,
					$value,
					$cat);
		$res = $db->query($strSQL);

    	return $res;
    }
	
	/* Input: 	- id and value position active of category need set position
	
	*/
	
	 public static function update_position_category($db, $cat,$value) {
    	$strSQL = sprintf("UPDATE  %s SET cat_position ='%d' " .
    					"WHERE cat_id = '%d'", 
					TBL_CATEGORY,
					$value,
					$cat);
		$res = $db->query($strSQL);

    	return $res;
    }
	
	/* Input: 	- id and value avtive of category need set default
	
	*/
	
	 public static function update_default_category($db, $cat,$value) {
    	$strSQL = sprintf("UPDATE  %s SET cat_default='%d' " .
    					"WHERE cat_id = '%d'", 
					TBL_CATEGORY,
					$value,
					$cat);
		
			
		$res = $db->query($strSQL);

    	return $res;
    }


	 public static function get_default_category($db) {
		$strSQL = sprintf("SELECT cat_id,cat_name FROM ".TBL_CATEGORY." WHERE cat_default = '1'");
    	$res = $db->get_row($strSQL);		
		return $res;
    }

 	public static function delete_category($db, $id) {
    	
    	//Delete category
    	$strSQL = sprintf("DELETE FROM %1\$s WHERE cat_id = '$id'", TBL_CATEGORY);
    	$res = $db->query($strSQL);

		$strSQL_assign = sprintf("DELETE FROM %1\$s WHERE category_id = '$id'", TBL_PAGE2CAT);
		$db->query($strSQL_assign);
    	
    	return $res;
    }
    
	public static function get_level($db, $catId) {
		$count = 0;
		$strSQL = "SELECT cat_parent FROM " . TBL_CATEGORY . " WHERE cat_id=%s";
		$parent = $db->get_var(sprintf($strSQL, $catId));
		while($parent > 0) {
			$count ++;
			$parent = $db->get_var(sprintf($strSQL, $parent));
		}
		return $count;
	}
	
	public static function get_category_by_page($db, $pageid){
    	$cat = null;
    	$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT category_id FROM %1\$s WHERE %1\$s.page_id=" . $pageid, TBL_PAGE2CAT);
    	$cat = $db->get_results($sql);
    	return $cat;
    }
    
    public function get_category_paging($db,$paging,$path,$id,$sort,$by) {
		if($sort!="")
			$condsort = "ORDER BY c.".$sort." ".$by;
		else
			$condsort = "ORDER BY c.cat_position ";
    	$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
		if($id!="")
			$cond = "c.cat_onleft = '1' AND c.cat_parent = '$id' " ;	
		else
			$cond = "c.cat_parent = '0'" ;	
    	$sql = sprintf("SELECT c.*,(SELECT COUNT(t.cat_parent) FROM ".TBL_CATEGORY." t WHERE t.cat_parent = c.cat_id) as subcat FROM ".TBL_CATEGORY." c " .
    	  				"WHERE $cond $condsort " .
						"LIMIT $page_start,$rows_per_page");
    	
    	return $db->get_results($sql);
    }
	
	
	public function count_category($db,$id) {
		if($id!="")
			$cond = "%1\$s.cat_onleft = '1' AND cat_parent = '$id' " ;	
		else
			$cond = "%1\$s.cat_parent = '0'" ;
		$sql = sprintf("SELECT COUNT(%1\$s.cat_id) as total FROM %1\$s WHERE $cond ", 
    			TBL_CATEGORY);
    	$res = $db->get_row($sql);
		return	$res->total;
	}
	
	public function count_top_category($db) {
		
		$sql = sprintf("SELECT COUNT(%1\$s.cat_id) as total FROM %1\$s WHERE cat_ontop ='1' ", 
    			TBL_CATEGORY);
    	$res = $db->get_row($sql);
		return	$res->total;
	}

	
//	public static function get_all_widget($db){
//    	$sql = sprintf("SELECT * FROM ".TBL_WIDGET); 
//    	$res = $db->get_results($sql);
//    	return $res;
//    }
	

}
?>